package com.sdkj.recruit.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件字节转换工具类
 * 
 * @author shenxiaozhong
 * @version 1.0
 * 
 *
 */
public class FileByteUtils {

	private static final Logger logger = LoggerFactory.getLogger(FileByteUtils.class);

	/**
	 * 文件转byte
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] getByte(String filePath) {

		byte[] buffer = null;

		try {
			File f = new File(filePath);
			FileInputStream in = new FileInputStream(f);
			ByteArrayOutputStream byout = new ByteArrayOutputStream(1000);
			byte[] a = new byte[1000];
			int n = 0;
			while ((n = in.read(a)) != -1) {
				byout.write(a, 0, n);
			}
			in.close();
			byout.close();
			buffer = byout.toByteArray();
		} catch (FileNotFoundException e) {
			logger.error("error :file not found -->" + e.fillInStackTrace());
		} catch (IOException e) {
			logger.error("error :io exection -->" + e.fillInStackTrace());
		}
		return buffer;
	}

	/**
	 * byte 转文件 文件写入缓冲区
	 * 
	 * @param a
	 * @param filePath
	 * @param fileName
	 */
	public static void getFile(byte[] a, String filePath, String fileName) {

		FileOutputStream out = null;
		BufferedOutputStream bos = null;

		try {

			File f = new File(filePath);
			if (!f.exists() && f.isDirectory()) {
				f.mkdirs();
			}
			f = new File(filePath + File.separator + fileName);
			out = new FileOutputStream(f);
			bos = new BufferedOutputStream(out);
			bos.write(a);

		} catch (FileNotFoundException e) {
			logger.error("error :file not found -->" + e.fillInStackTrace());
		} catch (IOException e) {
			logger.error("error :io exection -->" + e.fillInStackTrace());
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					logger.error("error :io exection -->" + e.fillInStackTrace());
				}
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					logger.error("error :io exection -->" + e.fillInStackTrace());
				}
			}
		}

	}

	/**
	 * 文件内容读取
	 * 
	 * @param path
	 * @param fileName
	 * @return
	 */
	public static List<String> fileReader(String path, String fileName) {

		File f = new File(path, fileName);
		if (!f.exists()) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String line = null;
		FileInputStream in = null;
		InputStreamReader inr = null;
		BufferedReader br = null;
		try {
			in = new FileInputStream(f);
			// 解决中文问题
			inr = new InputStreamReader(in, "gbk");
			br = new BufferedReader(inr);

			while ((line = br.readLine()) != null) {

				list.add(line);
				System.out.println(line);
			}
			br.close();
			inr.close();
			in.close();
		} catch (IOException e) {
			logger.error("error :io exection -->" + e.getMessage());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					logger.error("error :io exection -->" + e.getMessage());
				}
			}

			if (inr != null) {
				try {
					inr.close();
				} catch (IOException e) {
					logger.error("error :io exection -->" + e.getMessage());
				}
			}

			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("error :io exection -->" + e.getMessage());
				}
			}
		}
		return list;

	}

	public static void main(String[] args) {
		// getByte("E:\\test\\1.pdf");

		// getFile(getByte("E:\\test\\1.pdf"), "e:\\test\\", "3.pdf");
		fileReader("e:\\test\\", "test1.txt");

	}
}
